function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

function move() {
    //鼠标的移入移出
    $(".input_sub").mouseover(function () {
        $(this).css(
            "backgroundColor", "#0000FF"
        )
    }).mouseout(function () {
        $(this).prop(
            {
                "style": "display:block; padding-left:20px; padding-right:20px;" +
                    " line-height:40px;float:left; font-size:14px; color:#FFF;" +
                    " margin-left:213px"
            }
        )
    });
}


$(function () {
    move();

    var user_id = $('.username').html();
    var real_name = $(".realname").html();
    var nick_name = $(".nickname").html();
    var mobile = $(".mobile").html();
    var e_mail = $(".email").html();
    var birthday = $(".birthday").html();

    // TODO 编辑
    $(".bianji").click(function () {
        $(".base_info_form").html('');
        var content = "";
        content += '<div class="tirstbf">';
        content += '<em>用户名：</em>';
        content += '<div style="float:left; box-shadow:none; height:28px; font-size:16px; text-indent:6px; margin-top:68px">' + user_id + '</div>';
        content += '</div>';

        content += '<div class="thetwobf">';
        content += '<em>真实姓名：</em><input style="float:left; border:1px solid #bbb; box-shadow:none; height:28px; font-size:12px; text-indent:6px" type="text" class="shuru realname" placeholder="' + real_name + '" required/>';
        content += '</div>';
        content += '<div class="thetwobf">';
        content += '<em>昵称：</em><input style="float:left; border:1px solid #bbb; box-shadow:none; height:28px; font-size:12px; text-indent:6px" type="text" class="shuru nickname" placeholder="' + nick_name + '" required/>';
        content += '</div>';

        content += '<div class="thetwobf">';
        content += '<em>性别：</em>';
        content += '<input type="radio" name="sex" style=" float:left; display:block; width:13px; height:13px; margin-top:9px " value="MAN"><span>男</span>';
        content += '<input type="radio" name="sex" style=" float:left; display:block; width:13px; height:13px; margin-top:9px" value="WOMAN"><span>女</span>';
        content += '<input type="radio" name="sex" style=" float:left; display:block; width:13px; height:13px; margin-top:9px" value="SECRECY"><span>保密</span>';
        content += '</div>';

        content += '<div class="thetwobf">';
        content += '<em>电话：</em><input style="float:left; border:1px solid #bbb; box-shadow:none; height:28px; font-size:12px; text-indent:6px" type="text" class="shuru mobile" placeholder=" ' + mobile + '" required/>';
        content += '</div>';
        content += '<div class="thetwobf">';
        content += '<em>邮箱：</em><input style="float:left; border:1px solid #bbb; box-shadow:none; height:28px; font-size:12px; text-indent:6px; width:160px" type="text" class="shuru email" placeholder=" ' + e_mail + ' " required/>';
        content += '</div>';

        content += '<div class="thetwobf">';
        content += '<em>出生日期：</em><input style="float:left; border:1px solid #bbb; box-shadow:none; height:28px; font-size:12px; text-indent:6px; width:160px" type="text" class="shuru birthday" placeholder="' + birthday + '" required/>';
        content += '</div>';

        content += '<div class="thetwobf">';
        content += '<em>居住地址：</em><input type="text" class="addr" id="city" value="点击选择地区"  style=" height:28px; font-size:12px; border:1px solid #bbb; float:left">';
        content += '<em style=" width:60px">街道：</em>';
        content += '<input style="float:left; border:1px solid #bbb; box-shadow:none; height:28px; font-size:12px; text-indent:6px; width:420px" type="text" class="shuru" placeholder="" required/>';
        content += '</div>';
        content += '<div class="thetwobf">';
        content += '<input type="button" value="保 存" class="input_sub save" id="save" onclick="Baocun()" style=" display:block; padding-left:20px; padding-right:20px; line-height:40px;float:left; font-size:14px; color:#FFF; margin-left:213px">';
        content += '</div>';
        $(".base_info_form").html(content);
        $(".addr").click(function (e) {
            SelCity(this, e);
        });
        move();
    })
    // TODO 取消收藏
    $('.remove_collect').click(function () {
        var goods_id = $(this).parent().prev().html();
        params = {
            'goods_id': goods_id,
        }

        $.ajax({
            url: '/user/collect_remove',
            type: 'POST',
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            //把js对象转为json格式的字符串
            data: JSON.stringify(params),
            contentType: "application/json",
            dataType: 'json',
            success: function (data) {
                // 删除该条数据
                if (data.errno == '0') {
                    content = '';
                    $(this).parent().parent().html(content);
                    alert('删除成功!');
                    location.reload()
                } else {
                    alert(data.errmsg)
                }
            }, error: function (e) {
                alert('网络繁忙,请稍后再试!')
            }
        })
    })

    // TODO 购物车删除商品
    // TODO 取消收藏
    $('.shop_car_remove').click(function () {
        var goods_id = $(this).attr('goodsid');
        params = {
            'goods_id': goods_id,
        }

        $.ajax({
            url: '/user/shop_car_remove',
            type: 'POST',
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            //把js对象转为json格式的字符串
            data: JSON.stringify(params),
            contentType: "application/json",
            dataType: 'json',
            success: function (data) {
                // 删除该条数据
                if (data.errno == '0') {
                    content = '';
                    $(this).parent().parent().parent().html(content);
                    alert('删除成功!');
                    location.reload()
                } else {
                    alert(data.errmsg)
                }
            }, error: function (e) {
                alert('网络繁忙,请稍后再试!')
            }
        })
    })

    // TODO 付款
    $('.cost').click(function () {
        var goods_id = $(this).attr('goodsid');
        var g_count = 1
        $('.cost').prop(
            {
                'href': '/goods/order?g_id=' + goods_id + '&&g_count=' + g_count,
            }
        )

    })


})

// TODO 保存功能
function Baocun() {
    // 取到用户输入的内容

    var user_id = $('.username').val();
    var real_name = $(".realname").val();
    var nick_name = $(".nickname").val();
    var gender = $("input[name='sex']:checked").val();
    var mobile = $(".mobile").val();
    var e_mail = $(".email").val();
    var birthday = $(".birthday").val();
    var area_addr = $("#city").val();
    alert('点击保存');
    var params = {
        'user_id': user_id,
        'real_name': real_name,
        'nick_name': nick_name,
        'gender': gender,
        'e_mail': e_mail,
        'birthday': birthday,
        "mobile": mobile,
        'area_addr': area_addr,
    };

    $.ajax({
        url: "/user/base_user",
        type: "post",
        headers: {
            "X-CSRFToken": getCookie("csrf_token")
        },
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (data) {
            if (data.errno == "0") {
                $(".base_info_form").html();
                user_info = data.user_info;
                content = '';
                content += '<div class="tirstbf">';
                content += '<em>用户名：</em>';
                content += ' <div style="float:left; box-shadow:none; height:28px; font-size:16px; text-indent:6px; margin-top:68px">' + user_id + '</div>';
                content += '</div>';

                content += '<div class="thetwobf">';
                content += '<em>真实姓名：</em>';
                content += '<div style="float:left; font-size:12px; text-indent:6px;margin-top:8px" class="shuru realname">' + real_name + '</div>';
                content += '</div>';
                content += '<div class="thetwobf">';
                content += '<em>昵称：</em>';
                content += '<div style="float:left; font-size:12px; text-indent:6px;margin-top:8px" class="shuru nickname">' + nick_name + '</div>';
                content += '</div>';

                content += '<div class="thetwobf">';
                content += '<em>性别：</em>';
                content += '<div style="float:left; font-size:12px; text-indent:6px;margin-top:8px" class="shuru gender">' + gender + '</div>';
                content += '</div>';

                content += '<div class="thetwobf">';
                content += '<em>电话：</em>';
                content += '<div style="float:left; font-size:12px; text-indent:6px;margin-top:8px" class="shuru mobile">' + mobile + '</div>';
                content += '</div>';

                content += '<div class="thetwobf">';
                content += '<em>邮箱：</em>';
                content += '<div style="float:left; font-size:12px; text-indent:6px;margin-top:8px" class="shuru email">' + e_mail + '</div>';
                content += '</div>';

                content += '<div class="thetwobf">';
                content += '<em>居住地址：</em>';
                content += '<div style="float:left; font-size:12px; text-indent:6px;margin-top:8px" class="shuru" id="city">' + area_addr + '</div>';
                content += '</div>';
                content += '<div class="thetwobf">';
                content += '<input type="button" value="编 辑 " class="input_sub bianji" style=" display:block; padding-left:20px; padding-right:20px; line-height:40px;float:left; font-size:14px; color:#FFF; margin-left:213px">';
                content += '</div>';
                $(".base_info_form").html(content);
                move();
                // 刷新当前界面
                location.reload()
            } else {
                alert(data.errmsg)
            }
        }, error: function (e) {
            alert('保存繁忙,请稍后再试!')
        }
    })
}


// TODO 发送短信验证码
function sendSMSCode() {
    // 校验参数，保证输入框有数据填写
    $(".get_code").removeAttr("onclick");
    $('#register-mobile-err').html('')
    var mobile = $(".mobile").val();
    if (!mobile) {
        $("#register-mobile-err").html("请填写正确的手机号！");
        $("#register-mobile-err").show();
        $(".get_code").attr("onclick", "sendSMSCode();");
        return;
    }
    var param = {
        "mobile": mobile,
    }
    // TODO 发送短信验证码
    $.ajax({
        url: '/user/smscode',
        type: 'POST',
        headers: {
            "X-CSRFToken": getCookie("csrf_token")
        },
        //把js对象转为json格式的字符串
        data: JSON.stringify(param),
        contentType: "application/json",
        dataType: 'json',
        success: function (data) {
            if (data.errno == '0') {
                //    开始显示倒计时60秒
                alert('获取成功')
                var num = 60
                var t = setInterval(function () {
                    if (num == 1) {
                        //倒计时 到最后了
                        clearInterval(t)
                        $('.get_code').html('获取验证码')
                        //恢复点击
                        $(".get_code").attr("onclick", "sendSMSCode();");

                    } else {
                        //倒计时每次减1
                        num -= 1;
                        $('.get_code').html(num + '秒')
                    }
                }, 1000)

            } else {
                // 表示后端出现了错误，可以将错误信息展示到前端页面中
                $("#register-sms-code-err").html(data.errmsg);
                $("#register-sms-code-err").show();
                // 将点击按钮的onclick事件函数恢复回去
                $(".get_code").attr("onclick", "sendSMSCode();");

            }
        },
        error: function (e) {
            alert('网络不好，稍后再发')
        }
    });
}


//TODO 绑定手机号
function BindMobile() {
    //输入的手机号
    mobile = $('#mobile').val();
    smscode = $('#smscode').val();
    params = {
        'mobile': mobile,
        'smscode': smscode,
    };
    if (!smscode) {
        $("#register-sms-code-err").show();
        return;
    }
    $.ajax({
        url: '/user/binding',
        type: 'POST',
        headers: {
            "X-CSRFToken": getCookie("csrf_token")
        },
        //把js对象转为json格式的字符串
        data: JSON.stringify(params),
        contentType: "application/json",
        dataType: 'json',
        success: function (data) {
            if (data.errno == "0") {
                // 刷新当前界面
                alert('绑定成功')
                window.location.href = '/user/send_ok'
            } else {
                alert('已绑定')
            }
        }, error: function () {
            alert('网络错误！')
        }

    })
}